/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved.
 *
 */
import path from 'path';

import { expect } from '@jest/globals';
import fse from 'fs-extra';

import { runHvigorAsync } from '../../common/common';
import {
  AbilityType,
  APIType,
  APIVersion,
  CaseLevel,
  LanguageType,
  ModuleTemplateType,
  RuntimeOSType,
  SDKType,
} from '../../plugins/project-builder/models/constants';
import { TestCaseBuilder } from '../../plugins/task-builder/task-case-builder';
import { ProjectBuildOption, TestCaseFun } from '../../plugins/task-builder/test-case';
import { TestTaskOptionBuilder } from '../../plugins/task-builder/test-task-option';

const projectBuilderOption: ProjectBuildOption = {
  template: {
    sdk: SDKType.Single,
    runtimeOS: RuntimeOSType.HarmonyOS,
    apiVersion: APIVersion.API11,
    apiType: APIType.STAGE,
    language: LanguageType.ArkTs,
    abilityType: AbilityType.Application,
    entryTemplateType: ModuleTemplateType.EmptyAbility,
  },
  modulesOption: [ModuleTemplateType.SharedLibrary, ModuleTemplateType.FeatureEmptyAbility],
};

// eslint-disable-next-line max-lines-per-function
const testfun: TestCaseFun = async (project, taskConfig, record) => {
  const projectRootPath = project.projectRootPath();

  const module = project.getModule('entry');
  const moduleRootPath = module.moduleRootPath();
  const commands = '--mode module -p product=default -p module=entry@default default@GenerateLoaderJson'.split(' ');
  const artifactRoot = path.resolve(moduleRootPath, 'build/default/intermediates/loader/default/loader.json');

  const result1 = await runHvigorAsync(projectRootPath, commands);
  record.addStep('首次构建，GenerateMetadata为Finished', result1);
  expect(result1.stdout).toContain('BUILD SUCCESSFUL');
  expect(result1.stdout).toContain('Finished :entry:default@GenerateLoaderJson');
  expect(fse.existsSync(artifactRoot)).toEqual(true);
};

TestCaseBuilder.builder()
  .caseName('GenerateLoaderJson')
  .description('GenerateLoaderJson任务测试用例')
  .projectBuilderOption(projectBuilderOption)
  .taskConfig(TestTaskOptionBuilder.builder().caseLevel(CaseLevel.L2).build())
  .testFun(testfun)
  .load();
